package com.blstudio;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class viewSubjects extends Activity {
	ArrayAdapter<String> arrayAdapter = null;
	List<subjects> currentList = null;
	String[] subjects = null;
	String viewSubject;
	String boardURL;
	ListView subjectsListView;
	Intent webkitIntent;
	String baseURL = "http://nicegametv.ongamenet.com/bbs/";
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(android.R.style.Theme_Light);
		setContentView(R.layout.viewsubjects);
		
		
		/**
		 * 	viewSubject Param 
		 * 	1. URL for crawling subjects in board
		 * 	2. viewSubject String for viewSubject activity subject textview
		 */
		// get Param for viewer by intent
		this.getParam();

		currentList = HtmlParser.getInstance().getBoardSubjects( boardURL );
		subjects = new String[currentList.size()];
		
		int i = 0;
		
		for( subjects curr : currentList ){
			subjects[i++] = curr.getSubject();			
		}
		
		subjectsListView = (ListView) findViewById( R.id.subjectsListview );
		TextView test = (TextView) findViewById( R.id.subjectsSubect );
		test.setText(viewSubject);
		
    }

	public void onStart(){
		
		super.onStart();
		
		
		try {
			arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subjects );
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		subjectsListView.setAdapter(arrayAdapter);
		subjectsListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				webkitIntent = new Intent(viewSubjects.this, webkitContainer.class);
				webkitIntent.putExtra("URL", baseURL + currentList.get(arg2).getLinkURL());

				startActivity(webkitIntent);
				
			}
		  });	
	}
	
    @Override 
    public void onBackPressed() { 
    	finish();    
    }
    
    public void getParam(){
		Intent caller = getIntent();
		boardURL = caller.getStringExtra("categoryURL");		
		viewSubject = caller.getStringExtra("categoryString");		
		
    }
    
}
